package com.smart.controller.sys;

import java.util.Map;
import java.util.List;
import com.smart.vo.TreeSelect;
import com.smart.entity.SysDept;
import javax.annotation.Resource;
import io.swagger.annotations.Api;
import com.smart.util.CommonResult;
import com.smart.service.DeptService;
import io.swagger.annotations.ApiOperation;

import com.smart.common.annotation.SysLog;
import com.smart.common.enums.BusinessType;
import com.smart.controller.BaseController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lgb
 * @date 2020年6月11日 下午4:07:01
 * Copyright (C) 深圳工勘科技
 * @说明: 部门管理
 **/
@Api(tags="Sys-dept => 部门管理")
@RestController("sysDeptController")
@RequestMapping("sys/dept")
public class DeptController extends BaseController{

	@Resource
	private DeptService deptService;
	
	@ApiOperation("list => 根据关键字获取部门列表")
    @GetMapping(value="/list")
    public CommonResult<List<SysDept>> list(Map<String,Object> params) {
        List<SysDept> list = deptService.selectDeptList(params);
        return CommonResult.success(list);
    }

	@ApiOperation("{id} => 根据部门编号获取详细信息")
    @GetMapping(value="/{id}")
    public CommonResult<SysDept> getInfo(@PathVariable Long id) {
        return CommonResult.success(deptService.selectById(id));
    }
	
	@ApiOperation("update/{id} => 修改指定部门")
    @SysLog(title="部门管理",businessType=BusinessType.UPDATE)
    @PutMapping(value="/update/{id}")
    public CommonResult<Integer> update(@PathVariable Long id,
    		@RequestBody SysDept sysDept) {
        int count = deptService.update(id,sysDept);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }
	
	@ApiOperation("treeselect => 获取部门下拉树列表")
	@GetMapping(value="/treeselect")
	public CommonResult<List<TreeSelect>> treeselect(Map<String,Object> params) {
		List<SysDept> depts = deptService.selectDeptList(params);
		return CommonResult.success(deptService.buildDeptTreeSelect(depts));
	}
	
	@ApiOperation("create => 新建部门")
    @SysLog(title = "部门管理", businessType = BusinessType.INSERT)
    @PostMapping(value = "/create")
    public CommonResult<Integer> create(@RequestBody SysDept sysDept) {
        int count = deptService.create(sysDept);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }
	
	@ApiOperation("delete/{id} => 删除指定部门")
    @SysLog(title="部门管理",businessType=BusinessType.DELETE)
    @DeleteMapping(value="/delete/{id}")
    public CommonResult<Integer> delete(@PathVariable Long id) {
        int count = deptService.delete(id);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }
	
	
	
	
}